package server

func (c *Conn) writeInitialHandshake() error {
	data := make([]byte, 4)

	data = append(data, 10)

	data = append(data, c.serverConf.serverVersion...)
	data = append(data, 0x00)

	data = append(data, byte(c.connectionID), byte(c.connectionID>>8), byte(c.connectionID>>16), byte(c.connectionID>>24))

	data = append(data, c.salt[0:8]...)

	data = append(data, 0x00)

	defaultFlag := c.serverConf.capability
	data = append(data, byte(defaultFlag), byte(defaultFlag>>8))

	data = append(data, c.serverConf.collationId)

	data = append(data, byte(c.status), byte(c.status>>8))

	data = append(data, byte(defaultFlag>>16), byte(defaultFlag>>24))

	data = append(data, byte(8+12+1))

	data = append(data, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

	data = append(data, c.salt[8:]...)
	data = append(data, 0x00)

	data = append(data, c.serverConf.defaultAuthMethod...)

	data = append(data, 0)

	return c.WritePacket(data)
}
